//
//  ESTagsListCell.m
//  Eyeshot
//
//  Created by lanou3g on 15/7/8.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESTagsListCell.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
@implementation ESTagsListCell
- (instancetype)init{
    _times = 0;
    return self;
}
- (void)setTagsListModel:(ESTagsListModel *)tagsListModel{
    //给cell赋值
    [_coverImageView sd_setImageWithURL:[NSURL URLWithString:tagsListModel.image_list]];
    _theme_nameLable.text = tagsListModel.theme_name;
    _sub_numberLabel.text = [NSString stringWithFormat:@"%@人订阅",tagsListModel.sub_number];
    
    [_subscribleBT addTarget:self action:@selector(subBT:) forControlEvents:UIControlEventTouchUpInside];
    if (tagsListModel.is_default) {
        [_subscribleBT setTitle:@"取消订阅" forState:UIControlStateNormal];
        [_subscribleBT setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }else{
        [_subscribleBT setTitle:@"+ 订阅" forState:UIControlStateNormal];
        [_subscribleBT setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
}

- (void)subBT:(UIButton *)button{
//    NSLog(@"%@",_theme_id);
    if (_is_default) {
        //发送通知ESSubscribeViewController,显示提示视图,这是默认标签不能起效订阅.
        [[NSNotificationCenter defaultCenter] postNotificationName:@"alertView" object:@"ESSubscribeViewController" userInfo:nil];
    }else{
        if (_times % 2 == 0) {
            [_subscribleBT setTitle:@"取消订阅" forState:UIControlStateNormal];
            [_subscribleBT setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            //发送请求
            [self sendRequest:-1 theme_id:_theme_id];
        }else{
            [_subscribleBT setTitle:@"+ 订阅" forState:UIControlStateNormal];
            [_subscribleBT setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            //发送请求
            [self sendRequest:1 theme_id:_theme_id];
        }
    }
    _times ++;
}
#pragma mark - 发送请求

- (void)sendRequest:(NSInteger)status theme_id:(NSString *)theme_id{
    //1.获得管理者
    AFHTTPRequestOperationManager *netManager = [[AFHTTPRequestOperationManager alloc] init];
    //2.设置参数?a=tags&appname=baisishequ&asid=1880212C-B451-4FA7-8DFA-E07254EFCEE9&c=subscribe&client=iphone&device=iPhone%204S&format=json&jbk=0&mac=&market=&openudid=5a299ed728ffa000f0c4db0eb49e93d0e3968b2a&status=1&theme_id=163&udid=&ver=3.6
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"tags";
    parameters[@"appname"] = @"baisishequ";
    parameters[@"asid"] = @"1880212C-B451-4FA7-8DFA-E07254EFCEE9";
    parameters[@"c"] = @"subscribe";
    parameters[@"client"] = @"iphone";
    parameters[@"device"] = [UIDevice currentDevice].model;
    parameters[@"format"] = @"json";
    parameters[@"jbk"] = @"0";
    parameters[@"mac"] = @"";
    parameters[@"market"] = @"";
    parameters[@"openudid"] = @"5a299ed728ffa000f0c4db0eb49e93d0e3968b2a";
    parameters[@"status"] = [NSString stringWithFormat:@"%ld",status];//parameter = 1(订阅),=-1(取消订阅)
    parameters[@"theme_id"] = [NSString stringWithFormat:@"%@",theme_id];
    parameters[@"udid"] = @"";
    parameters[@"ver"] = @"3.6";
    //3.发送请求
    [netManager GET:@"http://api.budejie.com/api/api_open.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, NSDictionary *dic) {
        NSLog(@"发送成功:%@",dic);
        //发送成功,给ESSubscribeViewController发个通知,刷新表格
        [[NSNotificationCenter defaultCenter] postNotificationName:@"刷新表格" object:@"ESSubscribeViewController" userInfo:nil];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"发送失败:%@",error);
    }];
}

@end
